package com.webserver.template_core;

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Article {
    private static MutableDataSet options = new MutableDataSet();
    private static Parser parser = Parser.builder(options).build();
    private static HtmlRenderer renderer = HtmlRenderer.builder(options).build();

    private String fileName;
    private String pathName;
    private String subjectName;
    private long lastModified;
    private String content;
    private Map<String,String>metaData = new HashMap<>();

    public Article(File file){
        initFileInfo(file);
        initContentInfo(file);
    }

    private void initFileInfo(File file){
        System.out.println("初始化md文件消息");
        File parent = file.getParentFile();
        fileName = file.getName().replace(".md",".html");
        String [] data = parent.getName().split("_");
        pathName = data[0];
        subjectName = data[1];
        lastModified = file.lastModified();
        System.out.println("初始化md文件消息完毕！");
    }

    private void initContentInfo(File file){
        try(RandomAccessFile raf = new RandomAccessFile(file,"r");){
            byte[]data = new byte[(int)raf.length()];
            raf.read(data);
            String txt = new String(data,"utf-8");
            System.out.println(txt);

            //截取元数据(标题、时间、作者)
            int start = txt.indexOf("---")+3;
            int end = txt.indexOf("---",start);
            String metaStr = txt.substring(start,end).trim();
            System.out.println(metaStr);

            //每次读取一行
            Scanner scan = new Scanner(metaStr);
            while (scan.hasNextLine()){
                String line = scan.nextLine();
                if (line.trim().isEmpty()){
                    continue;
                }
                String[] arr = line.split(":\\s*");
                metaData.put(arr[0],arr[1]);
            }
            scan.close();
            System.out.println("metaData:"+metaData);

            //截取文章正文内容
            String contentStr = txt.substring(end+3);
            System.out.println("contentStr:"+contentStr);
            //md文件正文内容转换为html代码格式
            Node doc = parser.parse(contentStr);
            content = renderer.render(doc);
            System.out.println(content);


        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getPathName() {
        return pathName;
    }

    public void setPathName(String pathName) {
        this.pathName = pathName;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Map<String, String> getMetaData() {
        return metaData;
    }

    public void setMetaData(Map<String, String> metaData) {
        this.metaData = metaData;
    }
}
